package org.kjtc.controller;

import org.kjtc.ExportExcel.ExportReportExcel;
import org.kjtc.entity.BackLog;
import org.kjtc.service.ReportFormService;
import org.kjtc.web.JsonResult;
import org.kjtc.web.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/12/1 0001.
 */
@RestController
public class ReportFormController extends BaseController {

    @Autowired
    private ReportFormService reportFormService;

    @RequestMapping(value="doGetDateNavigations")
    public Map<String, Object> doGetDateNavigations(Date txtBeginDate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        Calendar cal = Calendar.getInstance();
        Map<String, Object> map = new HashMap<String, Object>();
        List list = new ArrayList();
        int Month = 0;
        int Week = 0;
        try {
            cal.setTime(txtBeginDate);
            //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
            if(1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
            int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            String imptimeBegin = sdf.format(cal.getTime());
            Week = cal.get(Calendar.WEEK_OF_YEAR);
            cal.add(Calendar.DATE, 1);
            String imptimeSe = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, 1);
            String imptimeMi = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, 1);
            String imptimeFo = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, 1);
            String imptimeEnd = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, 1);
            String imptimeSt = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, 1);
            String imptimeMo = sdf.format(cal.getTime());
            list=reportFormService.getDateNavigations(imptimeBegin,imptimeMo);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("list", list);
        map.put("week", Week);
        return map;
    }
    @RequestMapping(value="doGetBackLogByUserId")
    public JsonResult doGetAllBack(@RequestParam("userId")String userId){
        List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
        try {
            list = reportFormService.getAllBack(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return  new JsonResult(list);
        }
        return  new JsonResult(list);
    }

    @RequestMapping(value="doGetBackLogMsgDetalis")
    public Map<String, Object> doGetBackLogMsg(BackLog backLog){
        Map<String, Object> map = new HashMap<String, Object>();
        List<BackLog> listOne = null;
        List<BackLog> listTwo = null;
        try {
            listOne =  reportFormService.doGetBackLogMsgTwo(backLog);
            listTwo =  reportFormService.doGetBackLogMsgOne(backLog);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        if (this.getSession() != null) {
            this.getSession().setAttribute("listDone", listOne);
            this.getSession().setAttribute("listUndone", listTwo);
        }
        map.put("listOne", listOne);
        map.put("listTwo", listTwo);
        return map;
    }

    @RequestMapping(value="exportReport")
    public void exportReport(HttpServletResponse response, String txtBeginDate){
        List<BackLog> listDone = (List<BackLog>) this.getSession().getAttribute("listDone");
        List<BackLog> listUndone = (List<BackLog>) this.getSession().getAttribute("listUndone");
        ExportReportExcel<BackLog> exportReportExcel = new ExportReportExcel<BackLog>();
        exportReportExcel.run(listDone, listUndone, response, Tools.str2DTTM(txtBeginDate));
    }
}
